package odmd.handler;

import java.nio.ByteBuffer;

import odmd.Constants;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public final class Request {
	private byte magic = Constants.PROTOCOL_TYPE_REQUEST;
	private byte opcode = -1;
	private byte keepAlive = Constants.PROTOCOL_KEEPALIVE_YES;
	private byte reserved = 0x00;
	private byte[] cas = null;
	private int keyLength = 0;
	private long bodyLength = 0L;
	public byte getMagic() {
		return magic;
	}
	public void setMagic(byte magic) {
		this.magic = magic;
	}
	public byte getOpcode() {
		return opcode;
	}
	public void setOpcode(byte opcode) {
		this.opcode = opcode;
	}
	public byte getKeepAlive() {
		return keepAlive;
	}
	public void setKeepAlive(byte keepAlive) {
		this.keepAlive = keepAlive;
	}
	public byte getReserved() {
		return reserved;
	}
	public void setReserved(byte reserved) {
		this.reserved = reserved;
	}
	public byte[] getCas() {
		return cas;
	}
	public void setCas(byte[] cas) {
		this.cas = cas;
	}
	public void setCas(byte b) {
		if(this.cas == null) {
			this.cas = new byte[Constants.PROTOCOL_CAS_LENGTH];
		}
		for(int i = 0; i < cas.length; i++) {
			cas[i] = b;
		}
	}
	public void fill(ByteBuffer buff) {
		if(buff != null && buff.remaining() >= Constants.PROTOCOL_HEADER_LENGTH) {
			buff.put(magic);
			buff.put(opcode);
			buff.put(keepAlive);
			buff.put(reserved);
			if(cas != null) {
				buff.put(cas);
			} else {
				for(int i = 0; i < Constants.PROTOCOL_CAS_LENGTH; i++) {
					buff.put((byte)0);
				}
			}
			
			buff.putInt(keyLength);
			buff.putLong(bodyLength);
		}
	}
	public int getKeyLength() {
		return keyLength;
	}
	public void setKeyLength(int keyLength) {
		this.keyLength = keyLength;
	}
	public long getBodyLength() {
		return bodyLength;
	}
	public void setBodyLength(long bodyLength) {
		this.bodyLength = bodyLength;
	}
}
